package com.drunkensaints.android.happyhour.db;

import java.util.LinkedList;

import com.drunkensaints.android.happyhour.data.Location;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DbManager {
	private Context context;
	private DbHelper dbHelper;
	private SQLiteDatabase database;

	public DbManager(Context context) {
		this.context = context;
		dbHelper = new DbHelper(context);
	}
	
	public LinkedList<String> getLocationsSimpleList() {
		LinkedList<String> result = new LinkedList<String>();
		database = dbHelper.getReadableDatabase();
		Cursor qResult = database.query("locations", new String[] {"_id", "short_name"}, null, null, null, null, null);
		while(qResult.moveToNext()) {
			System.out.println("Short name: "+qResult.getString(1));
			result.add(qResult.getString(1));
		}
		database.close();
		return result;
	}
	
	public LinkedList<Location> getLocationsForStartScreen() {
		LinkedList<Location> result = new LinkedList<Location>();
		database = dbHelper.getReadableDatabase();
		Cursor qResult = database.query("locations", new String[]  {"_id", "short_name", "full_name", "address", "longtitude", "latitude", "site", "email", "short_description", "full_description", "priority", "is_active"}, "is_active=1", null, null, null, "priority desc");
		while(qResult.moveToNext()) {
			Location tmpItem = new Location();
			tmpItem._id = qResult.getInt(0);
			tmpItem.shortName = qResult.getString(1);
			tmpItem.fullName = qResult.getString(2);
			tmpItem.address = qResult.getString(3);
			tmpItem.longtitude = qResult.getFloat(4);
			tmpItem.latitude = qResult.getFloat(5);
			tmpItem.site = qResult.getString(6);
			tmpItem.email = qResult.getString(7);
			tmpItem.shortDescription = qResult.getString(8);
			tmpItem.fullDescription = qResult.getString(9);
			tmpItem.priority = qResult.getInt(10);
			tmpItem.isActive = qResult.getInt(11);
			result.add(tmpItem);
		}
		return result;
	}
}
